namespace MyCompany.UserInterfaceLibrary {
	using System.Windows;
	using System.Windows.Controls;

	public class DeluxeSeparator : Separator {
		/// <summary>
		///     Separates items in a horizontal or vertical toolbar, stackpanel, itemscontrol, etc. Orientation 'Horizontal' refers
		///     to separating horizontally aligned items, not to the shape of the separator. This defaults to
		///     Orientation.Horizontal.
		/// </summary>
		public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(DeluxeSeparator), new FrameworkPropertyMetadata(Orientation.Horizontal));

		/// <summary>
		///     Gets or sets the value of the <see cref="OrientationProperty" /> . This is a dependency property.
		/// </summary>
		public Orientation Orientation
		{
			get
			{
				return (Orientation)GetValue(OrientationProperty);
			}
			set
			{
				SetValue(OrientationProperty, value);
			}
		}

		public override void OnApplyTemplate() {
			base.OnApplyTemplate();

			UpdateState();
		}

		protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) {
			base.OnPropertyChanged(e);

			if (e.Property == OrientationProperty)
				UpdateState();
			else if (e.Property == ActualWidthProperty)
				UpdateState();
			else if (e.Property == ActualHeightProperty)
				UpdateState();
		}

		void UpdateState() {
			var border = (Border)GetTemplateChild("border");
			if (border != null)
				if (Orientation == Orientation.Horizontal)
					border.BorderThickness = new Thickness(2, 0, 0, 0);
				else
					border.BorderThickness = new Thickness(0, 2, 0, 0);
		}
	}
}
